package com.cl.security.system.service;

import com.cl.security.common.result.CommonResult;
import com.cl.security.system.entity.Permission;
import com.cl.security.system.repository.PermissionRepository;
import com.cl.security.base.service.TreeService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class PermissionService extends TreeService<Permission,PermissionRepository> {
    private final PermissionRepository permissionRepository;

    protected PermissionService(PermissionRepository permissionRepository) {
        super(permissionRepository);
        this.permissionRepository = permissionRepository;
    }

    public CommonResult page(int pageNumber, int pageSize, String contain, Integer menuId) {
        return CommonResult.success(permissionRepository.findAll((Specification<Permission>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (!contain.isEmpty()) {
                predicateList.add(criteriaBuilder.like(root.get("name"), "%" + contain + "%"));
            }
            if (menuId != null) {
                predicateList.add(criteriaBuilder.equal(root.get("menu"), menuId));
            }
            return criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()])).getRestriction();
        }, PageRequest.of(pageNumber - 1, pageSize)));
    }


    public boolean isExist(String path) {
        Specification<Permission> specification = new Specification<Permission>() {
            @Override
            public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("path"), path);
            }
        };
        Optional<Permission> permission = permissionRepository.findOne(specification);
        return permission.isPresent();
    }
}
